package com.itheima;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

/**
 * Map集合
 * （键值对集合）
 * Map(HashMap、HashTable)底层TreeMap
 * ----HashMap（LinkedHashMap）重要
 *
 * Map集合的键无序、不能重复、无索引，值可以重复
 * HashMap 集合的键值对无序、不能重复、无索引，值不做要求。
 *
 */
public class MapDemo {
    public static void main(String[] args) {
        test3();

    }

    private static void test0() {
        Map<String, Double> map = new HashMap<>();
        map.put("张三", 100.0);
        map.put("王五", 80.0);
        map.put("赵六", 90.0);
        map.put("张三", 95.0);//数据覆盖
        System.out.println(map);

        map.remove("张三");
        System.out.println(map);

        boolean b1 = map.containsKey("张三");
        System.out.println("b1="+b1);
        boolean b2 = map.containsValue(100.0);
        System.out.println("b2="+b2);
        boolean empty = map.isEmpty();
        System.out.println("empty="+empty);
        int size = map.size();
        System.out.println("size="+size);

        map.clear();
        System.out.println(map);
    }
    private static void test1() {
        Map<String, Double> map = new HashMap<>();
        map.put("张三", 100.0);
        map.put("王五", 80.0);
        map.put("赵六", 90.0);
        map.put("张三", 95.0);
        //将map中的所有键值取出来放到set集合中
        Set<String> keySet = map.keySet();
        //遍历key集合
        for (String key : keySet) {
            //根据key去找值
            Double value = map.get(key);
            System.out.println(key+"="+value);
        }
    }
    private static void test2() {
        Map<String, Double> map = new HashMap<>();
        map.put("张三", 100.0);
        map.put("王五", 80.0);
        map.put("赵六", 90.0);
        map.put("张三", 95.0);
        //1.将map集合转为set集合（将map中的key-value绑定为一个对象entry）
        Set<Map.Entry<String, Double>> entrySet = map.entrySet();
        //2.遍历set集合
        for (Map.Entry<String, Double> entry : entrySet) {
            //3.获取key
            String key = entry.getKey();
            //4.获取value
            Double value = entry.getValue();
            System.out.println(key+"="+value);
        }

    }
    private static void test3() {
        Map<String, Double> map = new HashMap<>();
        map.put("张三", 100.0);
        map.put("王五", 80.0);
        map.put("赵六", 90.0);
        map.put("张三", 95.0);
        map.forEach(new BiConsumer<String, Double>() {
            @Override
            public void accept(String key, Double value) {
                System.out.println(key+"="+value);

            }
        });
        //lambda表达式
        map.forEach((key, value) -> System.out.println(key+"="+value));


    }

}
